package controller;

import model.MySql;
import model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.util.Date;

/**
 * 更新用户个人信息
 * Created by yvdedu.com on 2016/9/27.
 */
@MultipartConfig
@WebServlet(urlPatterns = "/update_userinfo")
public class update_userinfo extends HttpServlet{
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        User user1=(User) req.getSession().getAttribute("user");
        if(user1!=null) {
            String id = String.valueOf(user1.getId());
            String nickname = req.getParameter("nickname");
            String sex = req.getParameter("sex");
            String age = req.getParameter("age");
            String phone = req.getParameter("phone");
            Part icon = req.getPart("icon");
            Boolean result = false;

            MySql mySql = new MySql();
            String path = req.getServletContext().getRealPath("/") + "\\icon\\";
            User user = mySql.getUserById(Integer.parseInt(id));
            if (icon.getSize() > 0) {
                String s = icon.getHeader("content-disposition");
                if (s != null && !s.trim().isEmpty() && (s.endsWith(".jpg\"") || s.endsWith(".png\"") || s.endsWith(".gif\"") || s.endsWith(".bmp\""))) {
                    s = String.valueOf(new Date().getTime());
                    result = mySql.setUserInfo(Integer.parseInt(id),"\\icon\\"+ s, nickname, sex, Integer.parseInt(age), phone);
                    if (result) {
                        try {
                            if (user.getIcon() != null || !user.getIcon().isEmpty()) {
                                File file = new File(user.getIcon());
                                if (file.exists()) file.delete();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        File file = new File(path);
                        if (!file.exists()) {
                            file.mkdirs();
                        }
                        icon.write(path + s);
                    }
                }else{
                    result = mySql.setUserInfo(Integer.parseInt(id),user.getIcon(), nickname, sex, Integer.parseInt(age), phone);
                }
            } else {
                result = mySql.setUserInfo(Integer.parseInt(id),user.getIcon(), nickname, sex, Integer.parseInt(age), phone);
            }
            if (result) {
                req.getSession().setAttribute("user", mySql.getUserById(Integer.parseInt(id)));
                resp.getWriter().print("修改成功");
            } else {
                resp.getWriter().print("修改失败");
            }
        }else{
            resp.getWriter().print("请登录后再操作");
        }
    }
}
